﻿using System;

namespace Com.Core.Helps
{
    public enum RandomCharType
    {
        Numbers = 1,
        Letters = 2,
        Charaters = 3
    }
    /// <summary>
    /// 生成随机数帮助类
    /// </summary>
    public class RandomHelper
    {
        private static readonly char[] Letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        private static readonly char[] Numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        private static readonly char[] Charaters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        private static char[] GetDictionary(RandomCharType type)
        {
            switch (type)
            {
                case RandomCharType.Charaters:
                    return Charaters;
                case RandomCharType.Letters:
                    return Letters;
                case RandomCharType.Numbers:
                    ; return Numbers;
            }
            return Charaters;
        }

        private static int CreateSeed()
        {
            return Guid.NewGuid().GetHashCode();
        }

        public static string CreateRandom(int length, RandomCharType type)
        {
            var dictionary = GetDictionary(type);
            var rdm  = new Random(CreateSeed());
            var str = new char[length];
            for (int i = 0; i < length; i++) { str[i] = dictionary[rdm.Next(dictionary.Length - 1)]; }
            return new string(str);
        }

        public static string CreateRandom(int minLength, int maxLength, RandomCharType type)
        {
            Random rdm = new Random(CreateSeed());
            int i = rdm.Next(minLength, maxLength);
            return CreateRandom(i, type);
        }
    }
}
